//"Parental" regressions:

/////////////////////////////////////////////////////////////////////////////
//Add military test score
/////////////////////////////////////////////////////////////////////////////
use "[Folder]\ParRegs_Data.dta"
merge 1:1 LopNr_PersonNr using "[Folder]\RARM.dta", keepusing(gkap STP2 STP3 STP4)
drop if _merge==2
drop _merge

save  "[Folder]\ParRegs_Data.dta", replace
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////

use "[Folder]\ParRegs_Data.dta"


drop if FodelseLandnamn!=107

gen For_F=0
replace For_F=1 if FodelseLandnamn_Far!=107 
gen For_M=0
replace For_M=1 if FodelseLandnamn_Mor!=107 

gen For2=0
replace For2 =1 if For_F==1  | For_M==1

drop if missing(FodelseLandnamn_Far) & missing(FodelseLandnamn_Mor)
drop if Kon==2

destring, replace


//Country-of-origin variables:
rename yr_sch_15992005_Mor yr_sch_05_Mor 
rename yr_sch_15992005_Far yr_sch_05_Far 

egen yr_sch_P = rowmean(yr_sch_05_Mor yr_sch_05_Far)
replace yr_sch_P = 0 if  For2==0

egen GDP_05_P  = rowmean(Ypwt2005_Far  Ypwt2005_Mor)
gen logGDP_P = log(GDP_05_P)
replace  logGDP_P = 0 if For2==0

egen cognitive_P  = rowmean(cognitive_Mor cognitive_Far)
replace  cognitive_P  = 0 if For2==0


replace Y003_Mor=(Y003_Mor+0.934001)/0.514  //Setting cross-country SD to 1.
replace Y003_Far=(Y003_Far+0.934001)/0.514
egen Y003_P = rowmean(Y003_Mor Y003_Far)
replace Y003_P=0 if For2==0

replace A165_Mor = (1-(A165_Mor-1))/0.1670898 //Setting cross-country SD to 1.
replace A165_Far = (1-(A165_Far-1))/0.1670898
egen A165_P = rowmean(A165_Mor A165_Far)
replace A165_P=0 if For2==0


egen TR_P = rowmean(TR_Mor TR_Far)
egen SS_P = rowmean(SS_Mor SS_Far)
replace TR_P  = 0 if For2==0
replace SS_P  = 0 if For2==0
  
egen SR_B00_P = rowmean(SR_B00_Mor SR_B00_Far)
replace SR_B00_P =0 if For2==0

gen Alder = 2014-Fodelsear
gen Alder_sq =  Alder^2
gen Alder_qu = Alder^3

egen AvW=rowmean(ForvInk_2014 ForvInk_2013 ForvInk_2012 ForvInk_2011)
gen logW=ln(AvW)

egen ClustGroup = group(FodelseLandnamn_Far FodelseLandnamn_Mor)


gen LonAlder_Mor_sq = LonAlder_Mor^2 
gen LonAlder_Far_sq = LonAlder_Far^2 

//Construct ed. variable
nsplit Sun2000niva, digits(1 1 1) generate(SunD1 SunD2 SunD3)
gen Educ_Ar = 0
replace Educ_Ar = 3 if SunD1==9
replace Educ_Ar = 6 if SunD1==1
replace Educ_Ar = 9 if SunD1==2
replace Educ_Ar = 10 if SunD1==3 & SunD2==1
replace Educ_Ar = 11 if SunD1==3 & SunD2==2

replace Educ_Ar = 12 if SunD1==3 & SunD2==3
replace Educ_Ar = 13 if SunD1==4 & SunD2==1
replace Educ_Ar = 14 if SunD1==5 & SunD2==2
replace Educ_Ar = 15 if SunD1==5 & SunD2==3
replace Educ_Ar = 16 if SunD1==5 & SunD2==4
replace Educ_Ar = 17 if SunD1==5 & SunD2==5
replace Educ_Ar = 18 if SunD1==6 & SunD2==0
replace Educ_Ar = 18 if SunD1==6 & SunD2==2
replace Educ_Ar = 20 if SunD1==6 & SunD2==4

drop SunD1 SunD2 SunD3


nsplit Sun2000niva_Far, digits(1 1 1) generate(SunD1 SunD2 SunD3)
gen Educ_Ar_Far = 0
replace Educ_Ar_Far = 3 if SunD1==9
replace Educ_Ar_Far = 6 if SunD1==1
replace Educ_Ar_Far = 9 if SunD1==2
replace Educ_Ar_Far = 10 if SunD1==3 & SunD2==1
replace Educ_Ar_Far = 11 if SunD1==3 & SunD2==2

replace Educ_Ar_Far = 12 if SunD1==3 & SunD2==3
replace Educ_Ar_Far = 13 if SunD1==4 & SunD2==1
replace Educ_Ar_Far = 14 if SunD1==5 & SunD2==2
replace Educ_Ar_Far = 15 if SunD1==5 & SunD2==3
replace Educ_Ar_Far = 16 if SunD1==5 & SunD2==4
replace Educ_Ar_Far = 17 if SunD1==5 & SunD2==5
replace Educ_Ar_Far = 18 if SunD1==6 & SunD2==0
replace Educ_Ar_Far = 18 if SunD1==6 & SunD2==2
replace Educ_Ar_Far = 20 if SunD1==6 & SunD2==4

drop SunD1 SunD2 SunD3

nsplit Sun2000niva_Mor, digits(1 1 1) generate(SunD1 SunD2 SunD3)
gen Educ_Ar_Mor = 0
replace Educ_Ar_Mor = 3 if SunD1==9
replace Educ_Ar_Mor = 6 if SunD1==1
replace Educ_Ar_Mor = 9 if SunD1==2
replace Educ_Ar_Mor = 10 if SunD1==3 & SunD2==1
replace Educ_Ar_Mor = 11 if SunD1==3 & SunD2==2

replace Educ_Ar_Mor = 12 if SunD1==3 & SunD2==3
replace Educ_Ar_Mor = 13 if SunD1==4 & SunD2==1
replace Educ_Ar_Mor = 14 if SunD1==5 & SunD2==2
replace Educ_Ar_Mor = 15 if SunD1==5 & SunD2==3
replace Educ_Ar_Mor = 16 if SunD1==5 & SunD2==4
replace Educ_Ar_Mor = 17 if SunD1==5 & SunD2==5
replace Educ_Ar_Mor = 18 if SunD1==6 & SunD2==0
replace Educ_Ar_Mor = 18 if SunD1==6 & SunD2==2
replace Educ_Ar_Mor = 20 if SunD1==6 & SunD2==4

drop SunD1 SunD2 SunD3


//Ability test
egen RekMyndAv = rowmean(STP2 STP3 STP4) 

gen InsProv = gkap
replace InsProv = RekMyndAv if missing(InsProv)




//sum AvWage_Mor if AvWage_Mor>0
replace AvWage_Mor = AvWage_Mor + 1 //To incl. individuals whose parents had zero income relevant years. Instead adjusting by +10, by + r(min), or hyperbolic sine, makes little difference. What makes a big difference is to incl. also individuals with zero earnings themselves (i.e. transforming the LHS variable as well). That almost double relevant coefficients on cultural values.
//sum AvWage_Far if AvWage_Far>0
replace AvWage_Far = AvWage_Far + 1

replace AvSP_Far = AvSP_Far  + 1
replace AvSP_Mor = AvSP_Mor  + 1


/*
sum AvSP_Mor if AvSP_Mor>0
replace AvSP_Mor = AvSP_Mor + r(min) //To include also individuals whose parents' did not receive sick-pay

sum AvSP_Far if AvSP_Far>0
replace AvSP_Far = AvSP_Far + r(min)
*/


gen log_SP_Far = ln(AvSP_Far)
gen log_SP_Mor = ln(AvSP_Mor)

gen log_AvW_Mor = ln(AvWage_Mor)
gen log_AvW_Far = ln(AvWage_Far)

//Gen indicator of WVInd_XXX=1 solely for Swedenborn parent; including would amount to result based solely on native-score.
gen WVSSwe=0
replace WVSSwe=1 if missing(WVInd_Far) & FodelseLandnamn_Mor==107
replace WVSSwe=1 if missing(WVInd_Mor) & FodelseLandnamn_Far==107



////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Output for table
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
cd "[Output Folder]\Output"


//Regs 
eststo clear
eststo: reg logW i.Kommun For2  TR_P SS_P  Alder Alder_sq Alder_qu   if !missing(cognitive_P)  & (!missing(WVInd_Mor) | !missing(WVInd_Far) ) & Fodelsear>1969 & Fodelsear<1989 & WVSSwe==0 , vce(cluster ClustGroup)

eststo: reg logW i.Kommun   Alder Alder_sq Alder_qu LonAlder_Far LonAlder_Far_sq LonAlder_Mor LonAlder_Mor_sq log_AvW_Far log_AvW_Mor  For2 TR_P SS_P  logGDP_P yr_sch_P  cognitive_P SR_B00_P    if !missing(cognitive_P)  & (!missing(WVInd_Mor) | !missing(WVInd_Far) ) & Fodelsear>1969 & Fodelsear<1989 & WVSSwe==0 , vce(cluster ClustGroup)

eststo: reg logW i.Kommun   Alder Alder_sq Alder_qu LonAlder_Far LonAlder_Far_sq LonAlder_Mor LonAlder_Mor_sq log_AvW_Far log_AvW_Mor  For2 TR_P SS_P  logGDP_P yr_sch_P  cognitive_P SR_B00_P  Educ_Ar_Far Educ_Ar_Mor if !missing(cognitive_P)  & (!missing(WVInd_Mor) | !missing(WVInd_Far) ) & Fodelsear>1969 & Fodelsear<1989 & WVSSwe==0 , vce(cluster ClustGroup)

eststo: reg logW i.Kommun   Alder Alder_sq Alder_qu LonAlder_Far LonAlder_Far_sq LonAlder_Mor LonAlder_Mor_sq log_AvW_Far log_AvW_Mor  For2 TR_P SS_P  logGDP_P yr_sch_P  cognitive_P SR_B00_P  Educ_Ar_Far Educ_Ar_Mor log_SP_Far log_SP_Mor Educ_Ar if !missing(cognitive_P)  & (!missing(WVInd_Mor) | !missing(WVInd_Far) ) & Fodelsear>1969 & Fodelsear<1989 & WVSSwe==0 , vce(cluster ClustGroup)

eststo: reg logW i.Kommun   Alder Alder_sq Alder_qu LonAlder_Far LonAlder_Far_sq LonAlder_Mor LonAlder_Mor_sq log_AvW_Far log_AvW_Mor  For2 TR_P SS_P  logGDP_P yr_sch_P  cognitive_P SR_B00_P  Educ_Ar_Far Educ_Ar_Mor log_SP_Far log_SP_Mor Educ_Ar InsProv if !missing(cognitive_P)  & (!missing(WVInd_Mor) | !missing(WVInd_Far) ) & Fodelsear>1969 & Fodelsear<1989 & WVSSwe==0 , vce(cluster ClustGroup)

esttab using ParRegs_TR.csv, drop(*.Kommun) order( Alder Alder_sq Alder_qu LonAlder_Far LonAlder_Far_sq LonAlder_Mor LonAlder_Mor_sq log_AvW_Far log_AvW_Mor  For2 TR_P SS_P  logGDP_P yr_sch_P  cognitive_P SR_B00_P  Educ_Ar_Far Educ_Ar_Mor log_SP_Far log_SP_Mor Educ_Ar InsProv )   replace


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



//Regs 
eststo clear
eststo: reg logW i.Kommun For2  Y003_P A165_P  Alder Alder_sq Alder_qu   if !missing(cognitive_P)  & (!missing(WVInd_Mor) | !missing(WVInd_Far) ) & Fodelsear>1969 & Fodelsear<1989 & WVSSwe==0 , vce(cluster ClustGroup)

eststo: reg logW i.Kommun   Alder Alder_sq Alder_qu LonAlder_Far LonAlder_Far_sq LonAlder_Mor LonAlder_Mor_sq log_AvW_Far log_AvW_Mor  For2 Y003_P A165_P  logGDP_P yr_sch_P  cognitive_P SR_B00_P    if !missing(cognitive_P)  & (!missing(WVInd_Mor) | !missing(WVInd_Far) ) & Fodelsear>1969 & Fodelsear<1989 & WVSSwe==0 , vce(cluster ClustGroup)

eststo: reg logW i.Kommun   Alder Alder_sq Alder_qu LonAlder_Far LonAlder_Far_sq LonAlder_Mor LonAlder_Mor_sq log_AvW_Far log_AvW_Mor  For2 Y003_P A165_P  logGDP_P yr_sch_P  cognitive_P SR_B00_P  Educ_Ar_Far Educ_Ar_Mor if !missing(cognitive_P)  & (!missing(WVInd_Mor) | !missing(WVInd_Far) ) & Fodelsear>1969 & Fodelsear<1989 & WVSSwe==0 , vce(cluster ClustGroup)

eststo: reg logW i.Kommun   Alder Alder_sq Alder_qu LonAlder_Far LonAlder_Far_sq LonAlder_Mor LonAlder_Mor_sq log_AvW_Far log_AvW_Mor  For2 Y003_P A165_P  logGDP_P yr_sch_P  cognitive_P SR_B00_P  Educ_Ar_Far Educ_Ar_Mor log_SP_Far log_SP_Mor Educ_Ar if !missing(cognitive_P)  & (!missing(WVInd_Mor) | !missing(WVInd_Far) ) & Fodelsear>1969 & Fodelsear<1989 & WVSSwe==0 , vce(cluster ClustGroup)

eststo: reg logW i.Kommun   Alder Alder_sq Alder_qu LonAlder_Far LonAlder_Far_sq LonAlder_Mor LonAlder_Mor_sq log_AvW_Far log_AvW_Mor  For2 Y003_P A165_P  logGDP_P yr_sch_P  cognitive_P SR_B00_P  Educ_Ar_Far Educ_Ar_Mor log_SP_Far log_SP_Mor Educ_Ar InsProv if !missing(cognitive_P)  & (!missing(WVInd_Mor) | !missing(WVInd_Far) ) & Fodelsear>1969 & Fodelsear<1989 & WVSSwe==0 , vce(cluster ClustGroup)

esttab using ParRegs_YA.csv, drop(*.Kommun) order( Alder Alder_sq Alder_qu LonAlder_Far LonAlder_Far_sq LonAlder_Mor LonAlder_Mor_sq log_AvW_Far log_AvW_Mor  For2 Y003_P A165_P  logGDP_P yr_sch_P  cognitive_P SR_B00_P  Educ_Ar_Far Educ_Ar_Mor log_SP_Far log_SP_Mor Educ_Ar InsProv )   replace



